﻿using System;
using System.Collections.Generic;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class FormulaFunction_BuffStack : FormulaFunction
    {
        public override EFormulaFunctionType funcType => EFormulaFunctionType.bBuffStack;

        public override void Calculate(Stack<IFormulaOperand> rCalopd, IFormulaObject rSelf, IFormulaObject rTarget, Formula rFromFormula)
        {
            IFormulaOperand opd1 = rCalopd.Pop();
            IFormulaOperand opd2 = rCalopd.Pop();
            if (opd1.TryGet<LFloat>(out var op1, rSelf, rTarget, rFromFormula) && opd2.TryGet<LFloat>(out var op2, rSelf, rTarget, rFromFormula))
            {
                var nBuffID = op2.ToInt();
                var bSelf = op1.ToInt() == 1;
                var nStack = (bSelf ? rSelf : rTarget).GetBuffStack(nBuffID);
                IFormulaOperand result = FormulaHelper.FetchOperand(EFormulaOperandType.Num);
                result.Set(nStack.ToLFloat());
                rCalopd.Push(result);
            }
            else
            {
                throw new Exception($"{funcType} func need two number");
            }

            opd1.TryFree();
            opd2.TryFree();
        }
    }
}